/**
* Class to manage an incide insertion into DB to be executed in the Servlet
*
* Project: idbcrown
* Author:  TM
* Date:    22/12/2012
*
* History
* ------------------
* 22/12/2012 - TM - Initial version.
*/
 
 
package Functions;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import Application.*;
import comm.*;

public class insertValue extends genericFunction implements constantsInterface
{
   /** Variables */
   protected String valueIdDevice;
   protected String valueIdMeasure;
   protected String valueValue;      

     /** Constructor  */
    public insertValue(HttpServletRequest _request, HttpServletResponse _response) throws IOException, ServletException
    {
       super(_request, _response);
    }
    
    /** Execute function */
    public String execute()
    {
       String  result  = "";
       boolean isOk    = true;
       valueIdDevice   = getRequest().getParameter(idDevice);     
       valueIdMeasure  = getRequest().getParameter(idMeasure);            
       valueValue      = getRequest().getParameter(value);            
       
       debug("Inserting value for idDevice "+valueIdDevice+"/idMeasure "+valueIdMeasure);
      
       try
       {
         debug("Inserted value excuted with result: "+isOk);        
         result = "Inserted value excuted with result: "+isOk+" for "+idDevice+"="+valueIdDevice+";"+idMeasure+"="+valueIdMeasure+";"+value+"="+valueValue;
       }
       catch(Exception ex)
       {
         sendError(ex);
       }
       
       return result;
    }
    
    /** Calls to the DBServer for inserting values */
    protected void callDBServerInsertValue()
    {
       servletDBComm dbServlet   = servletDBComm.getInstance();
       String        actionParam = insertAction;
       String[]      names       = {idDevice,      idMeasure,      value};
       String[]      values      = {valueIdDevice, valueIdMeasure, valueValue};
       try
       {
         String params = dbServlet.getFormattedParameters(actionParam, names, values);
         String result = dbServlet.post_get(params);
         sendResponse(result);
       }
       catch(Exception ex)
       {
         sendError(ex);
       }
    }
}